/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ugc.cnel.manabi.jpa;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import org.ugc.cnel.manabi.entidades.Servicio;
import java.util.ArrayList;
import java.util.Collection;
import org.ugc.cnel.manabi.entidades.TipoServicio;
import org.ugc.cnel.manabi.jpa.exceptions.IllegalOrphanException;
import org.ugc.cnel.manabi.jpa.exceptions.NonexistentEntityException;
import org.ugc.cnel.manabi.jpa.exceptions.PreexistingEntityException;

/**
 *
 * @author Admin
 */
public class TipoServicioJpaController {

    public TipoServicioJpaController() {
        emf = Persistence.createEntityManagerFactory("UGC_CNELPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(TipoServicio tipoServicio) throws PreexistingEntityException, Exception {
        if (tipoServicio.getServicioCollection() == null) {
            tipoServicio.setServicioCollection(new ArrayList<Servicio>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Servicio> attachedServicioCollection = new ArrayList<Servicio>();
            for (Servicio servicioCollectionServicioToAttach : tipoServicio.getServicioCollection()) {
                servicioCollectionServicioToAttach = em.getReference(servicioCollectionServicioToAttach.getClass(), servicioCollectionServicioToAttach.getId());
                attachedServicioCollection.add(servicioCollectionServicioToAttach);
            }
            tipoServicio.setServicioCollection(attachedServicioCollection);
            em.persist(tipoServicio);
            for (Servicio servicioCollectionServicio : tipoServicio.getServicioCollection()) {
                TipoServicio oldIdTipoServicioOfServicioCollectionServicio = servicioCollectionServicio.getIdTipoServicio();
                servicioCollectionServicio.setIdTipoServicio(tipoServicio);
                servicioCollectionServicio = em.merge(servicioCollectionServicio);
                if (oldIdTipoServicioOfServicioCollectionServicio != null) {
                    oldIdTipoServicioOfServicioCollectionServicio.getServicioCollection().remove(servicioCollectionServicio);
                    oldIdTipoServicioOfServicioCollectionServicio = em.merge(oldIdTipoServicioOfServicioCollectionServicio);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findTipoServicio(tipoServicio.getId()) != null) {
                throw new PreexistingEntityException("TipoServicio " + tipoServicio + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(TipoServicio tipoServicio) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TipoServicio persistentTipoServicio = em.find(TipoServicio.class, tipoServicio.getId());
            Collection<Servicio> servicioCollectionOld = persistentTipoServicio.getServicioCollection();
            Collection<Servicio> servicioCollectionNew = tipoServicio.getServicioCollection();
            List<String> illegalOrphanMessages = null;
            for (Servicio servicioCollectionOldServicio : servicioCollectionOld) {
                if (!servicioCollectionNew.contains(servicioCollectionOldServicio)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Servicio " + servicioCollectionOldServicio + " since its idTipoServicio field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Servicio> attachedServicioCollectionNew = new ArrayList<Servicio>();
            for (Servicio servicioCollectionNewServicioToAttach : servicioCollectionNew) {
                servicioCollectionNewServicioToAttach = em.getReference(servicioCollectionNewServicioToAttach.getClass(), servicioCollectionNewServicioToAttach.getId());
                attachedServicioCollectionNew.add(servicioCollectionNewServicioToAttach);
            }
            servicioCollectionNew = attachedServicioCollectionNew;
            tipoServicio.setServicioCollection(servicioCollectionNew);
            tipoServicio = em.merge(tipoServicio);
            for (Servicio servicioCollectionNewServicio : servicioCollectionNew) {
                if (!servicioCollectionOld.contains(servicioCollectionNewServicio)) {
                    TipoServicio oldIdTipoServicioOfServicioCollectionNewServicio = servicioCollectionNewServicio.getIdTipoServicio();
                    servicioCollectionNewServicio.setIdTipoServicio(tipoServicio);
                    servicioCollectionNewServicio = em.merge(servicioCollectionNewServicio);
                    if (oldIdTipoServicioOfServicioCollectionNewServicio != null && !oldIdTipoServicioOfServicioCollectionNewServicio.equals(tipoServicio)) {
                        oldIdTipoServicioOfServicioCollectionNewServicio.getServicioCollection().remove(servicioCollectionNewServicio);
                        oldIdTipoServicioOfServicioCollectionNewServicio = em.merge(oldIdTipoServicioOfServicioCollectionNewServicio);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tipoServicio.getId();
                if (findTipoServicio(id) == null) {
                    throw new NonexistentEntityException("The tipoServicio with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            TipoServicio tipoServicio;
            try {
                tipoServicio = em.getReference(TipoServicio.class, id);
                tipoServicio.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tipoServicio with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Servicio> servicioCollectionOrphanCheck = tipoServicio.getServicioCollection();
            for (Servicio servicioCollectionOrphanCheckServicio : servicioCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This TipoServicio (" + tipoServicio + ") cannot be destroyed since the Servicio " + servicioCollectionOrphanCheckServicio + " in its servicioCollection field has a non-nullable idTipoServicio field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(tipoServicio);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<TipoServicio> findTipoServicioEntities() {
        return findTipoServicioEntities(true, -1, -1);
    }

    public List<TipoServicio> findTipoServicioEntities(int maxResults, int firstResult) {
        return findTipoServicioEntities(false, maxResults, firstResult);
    }

    private List<TipoServicio> findTipoServicioEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from TipoServicio as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public TipoServicio findTipoServicio(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(TipoServicio.class, id);
        } finally {
            em.close();
        }
    }

    public int getTipoServicioCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from TipoServicio as o").getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
